﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;

using BLL;
using DTO;

namespace CRM.GUI.QLKhachHang
{
    public partial class frmDupcateDatabase : DevExpress.XtraEditors.XtraForm
    {
        public frmDupcateDatabase()
        {
            InitializeComponent();
            IntialInterFace();
            IntialLoad();
            frmDuplicateReport.LoadDeDupList = new frmDuplicateReport.LoadDeDup(LoadDeDupList);
        }
        private void IntialInterFace()
        {
            
            grdViewDupDatabase.OptionsView.ColumnAutoWidth = false;
            grdViewDupDatabase.ScrollStyle = ScrollStyleFlags.LiveHorzScroll | ScrollStyleFlags.LiveVertScroll;
            grdViewDupDatabase.HorzScrollVisibility = ScrollVisibility.Always;
            
            grdViewDupExcel.OptionsView.ColumnAutoWidth = false;
            grdViewDupExcel.ScrollStyle = ScrollStyleFlags.LiveHorzScroll | ScrollStyleFlags.LiveVertScroll;
            grdViewDupExcel.HorzScrollVisibility = ScrollVisibility.Always;

            grdColCounterDB.UnboundType = DevExpress.Data.UnboundColumnType.Integer;
            grdViewDupDatabase.CustomUnboundColumnData += grdViewDupDatabase_CustomUnboundColumnData;
            ColCounterExcel.UnboundType = DevExpress.Data.UnboundColumnType.Integer;
            grdViewDupExcel.CustomUnboundColumnData += grdViewDupExcel_CustomUnboundColumnData;
        }

        void grdViewDupDatabase_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.IsGetData)
                e.Value = e.ListSourceRowIndex + 1;
        }

        void grdViewDupExcel_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.IsGetData)
                e.Value = e.ListSourceRowIndex + 1;
        }
        
        private void IntialLoad()
        {
            // Database View
            cmbCity.DataSource = SharedData.RegionList.Where(a => a.RegionLevel == 2).ToList();
            cmbCity.DisplayMember = "RegionName";
            cmbCity.ValueMember = "Region_Code";
            cmbDistrict.DataSource = SharedData.RegionList.Where(a => a.RegionLevel == 3).ToList();
            cmbDistrict.DisplayMember = "RegionName";
            cmbDistrict.ValueMember = "Region_Code";
            cmbWard.DataSource = SharedData.RegionList.Where(a => a.RegionLevel == 4).ToList();
            cmbWard.DisplayMember = "RegionName";
            cmbWard.ValueMember = "Region_Code";
            cmbCollector.DataSource = SharedData.CollectorList;
            cmbCollector.DisplayMember = "Collector_Code";
            cmbCollector.ValueMember = "Collector_ID";
            cmbCurrentBrand.DataSource = SharedData.BrandList;
            cmbCurrentBrand.DisplayMember = "Brand_Code";
            cmbCurrentBrand.ValueMember = "Brand_ID";
            cmbPreviousBrand.DataSource = SharedData.BrandList;
            cmbPreviousBrand.DisplayMember = "Brand_Code";
            cmbPreviousBrand.ValueMember = "Brand_ID";

            // Excel View
            cmbCityExcel.DataSource = SharedData.RegionList.Where(a => a.RegionLevel == 2).ToList();
            cmbCityExcel.DisplayMember = "RegionName";
            cmbCityExcel.ValueMember = "Region_Code";
            cmbDistrictExcel.DataSource = SharedData.RegionList.Where(a => a.RegionLevel == 3).ToList();
            cmbDistrictExcel.DisplayMember = "RegionName";
            cmbDistrictExcel.ValueMember = "Region_Code";
            cmbWardExcel.DataSource = SharedData.RegionList.Where(a => a.RegionLevel == 4).ToList();
            cmbWardExcel.DisplayMember = "RegionName";
            cmbWardExcel.ValueMember = "Region_Code";
            cmbCollectorExcel.DataSource = SharedData.CollectorList;
            cmbCollectorExcel.DisplayMember = "Collector_Code";
            cmbCollectorExcel.ValueMember = "Collector_ID";
            cmbCurrentBrandExcel.DataSource = SharedData.BrandList;
            cmbCurrentBrandExcel.DisplayMember = "Brand_Code";
            cmbCurrentBrandExcel.ValueMember = "Brand_ID";
            cmbPreviousBrandExcel.DataSource = SharedData.BrandList;
            cmbPreviousBrandExcel.DisplayMember = "Brand_Code";
            cmbPreviousBrandExcel.ValueMember = "Brand_ID";

        }

        private List<DataTable> _dtDeDupList;
        private void LoadDeDupList(List<DataTable> ds)
        {
            _dtDeDupList = ds;            
            grdDupDatabase.DataSource = _dtDeDupList[1];
            grdDupExcel.DataSource = _dtDeDupList[0];
            if (_dtDeDupList[1] != null && _dtDeDupList[1].Rows.Count > 0)
            {
                lblCountDup.Text = _dtDeDupList[1].Rows.Count.ToString();
            }
            else
            {
                lblCountDup.Text = "0";
            }
            grdViewDupDatabase.BestFitColumns();
            grdViewDupExcel.BestFitColumns();
        }

        private void frmDupcateDatabase_FormClosed(object sender, FormClosedEventArgs e)
        {
            DataTranferEntity trans = new DataTranferEntity();
            FilterFunc filter = new FilterFunc();
            filter.DeleteDedup((grdDupExcel.DataSource as DataTable));
            trans.Source.TotalDeDup = int.Parse(lblCountDup.Text);
        }

        private void btnExportToXlsx_Click(object sender, EventArgs e)
        {
            grdViewDupExcel .OptionsPrint.AutoWidth = false;
            grdViewDupExcel.BestFitColumns();
            SaveFileDialog SaveDlg = new SaveFileDialog();
            SaveDlg.Filter = "*.xls|*.xlsx";
            SaveDlg.FileName = "Report.xlsx";
            if (SaveDlg.ShowDialog() != DialogResult.Cancel)
            {

                DevExpress.XtraPrinting.XlsxExportOptions opt = new DevExpress.XtraPrinting.XlsxExportOptions()
                {
                    TextExportMode = DevExpress.XtraPrinting.TextExportMode.Text,
                    ExportMode = DevExpress.XtraPrinting.XlsxExportMode.SingleFile,

                    ShowGridLines = true,
                };
                grdViewDupExcel.ExportToXlsx(SaveDlg.FileName.ToString(), opt);
            }
        }

    }
}